//
//  CLCollectionView.m
//  Charles
//
//  Created by Charles on 2017/4/5.
//  Copyright © 2017年 charles. All rights reserved.
//

#import "CLCollectionView.h"


@interface CLCollectionView()

@property (nonatomic, strong) UICollectionViewLayout *flowLayout;

@end

@implementation CLCollectionView

- (instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(nonnull UICollectionViewLayout *)layout {
    self = [super initWithFrame:frame];
    if (self) {
        self.flowLayout = layout;
        [self setUpView];
    }
    return self;
}

- (void)registerClass:(Class)cellClass forCellWithReuseIdentifier:(NSString *)identifier {
    [self.collectionView registerClass:cellClass forCellWithReuseIdentifier:identifier];
}

- (void)setUpView {
    [self.collectionView setBackgroundColor:[UIColor clearColor]];
}

-(void)setDelegate:(id)delegate {
    self.collectionView.delegate = delegate;
}

- (void)setDataSource:(id)dataSource {
    self.collectionView.dataSource = dataSource;
}

- (void)setScrollEnable:(BOOL)enable {
    self.collectionView.scrollEnabled = enable;
}

- (void)reloadItemsAtIndexPaths:(NSArray *)indexPaths {
    [self.collectionView reloadItemsAtIndexPaths:indexPaths];
}

- (void)reloadData {
    [self.collectionView reloadData];
}

- (void)setPageEnable:(BOOL)enable {
    [self.collectionView setPagingEnabled:enable];
}

- (void)setContentOffSet:(CGPoint)offset {
    [self.collectionView setContentOffset:offset];
}

- (void)setDecelerationRate:(CGFloat)rate {
    [self.collectionView setDecelerationRate:rate];
}

- (void)setCollectionviewTag:(NSInteger)tag {
    self.collectionView.tag = tag;
}

- (void)scrollToItemAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UICollectionViewScrollPosition)scrollPosition animated:(BOOL)animated {
    [self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:scrollPosition animated:animated];
}


- (nullable NSIndexPath *)clIndexPathForItemAtPoint:(CGPoint)point {
   return [self.collectionView indexPathForItemAtPoint:point];
}



- (UICollectionView *)collectionView {
    if (!_collectionView) {
        if (self.flowLayout) {
            _collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:self.flowLayout];
            _collectionView.showsHorizontalScrollIndicator = NO;
            [self addSubview:_collectionView];
        }
        
    }
    return _collectionView;
}

#pragma mark - 这些方法需要在子类中重写

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 0;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeZero;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    return nil;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
}

@end
